using MediatR;
using Medical.Api.Write.Application.Command.BasicManagement.DeviceClassificationDictionaryCommand;
using Medical.Api.Write.Application.Command.BasicManagement.DeviceDictionaryCommand;
using Medical.Api.Write.Application.Command.BasicManagement.EquipmentPackageDictionaryCommand;
using Medical.Api.Write.Application.Command.BasicManagement.ShelfDictionaryCommand;
using Medical.Api.Write.Dto.BasicManagement.DeviceClassificationDictionaryDto;
using Medical.Api.Write.Dto.BasicManagement.DeviceDictionaryDto;
using Medical.Api.Write.Dto.BasicManagement.EquipmentPackageDictionaryDto;
using Medical.Api.Write.Dto.BasicManagement.ShelfDictionaryDto;
using Medical.ErrorCode;
using Microsoft.AspNetCore.Mvc;

namespace Medical.Api.Write.Controllers
{
    /// <summary>
    /// 基础管理控制器
    /// 提供货架管理等基础数据操作的API接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BasicManagementController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<BasicManagementController> logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">MediatR中介者</param>
        /// /// <param name="logger">NLog日志</param>
        public BasicManagementController(IMediator mediator, ILogger<BasicManagementController> logger)
        {
            _mediator = mediator;
            this.logger = logger;
        }

        #region 设备字典

        /// <summary>
        /// 添加设备
        /// </summary>
        /// <param name="createEquipmentDto">设备数据</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public ApiResult<int> AddEquipment([FromBody] CreateEquipmentDto createEquipmentDto)
        {
            try
            {
                var command = new CreateEquipmentCommand(createEquipmentDto);
                var result = _mediator.Send(command).GetAwaiter().GetResult();

                logger.LogInformation("设备添加请求处理完成");

                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "处理添加设备请求时发生异常");
                return new ApiResult<int>
                {
                    Code = ApiEnums.Error,
                    Msg = $"处理请求时发生错误: {ex.Message}",
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 更新设备状态
        /// </summary>
        /// <param name="statusDto">设备状态更新数据</param>
        /// <returns>操作结果</returns>
        [HttpPut]
        public async Task<ApiResult<bool>> UpdateEquipmentStatus([FromBody] UpdateEquipmentStatusDto statusDto)
        {
            try
            {
                var command = new UpdateEquipmentStatusCommand(statusDto);
                var result = await _mediator.Send(command);

                logger.LogInformation("设备状态更新请求处理完成");

                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "处理更新设备状态请求时发生异常");
                return new ApiResult<bool>
                {
                    Code = ApiEnums.Error,
                    Msg = $"处理请求时发生错误: {ex.Message}",
                    Data = false
                };
            }
        }

        /// <summary>
        /// 删除设备(逻辑删除)
        /// </summary>
        /// <param name="id">设备ID</param>
        /// <returns>操作结果</returns>
        [HttpDelete("{id}")]
        public async Task<ApiResult<bool>> DeleteEquipment(int id)
        {
            try
            {
                var command = new DeleteEquipmentCommand { Id = id };
                var result = await _mediator.Send(command);

                logger.LogInformation("设备删除请求处理完成");

                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "处理删除设备请求时发生异常");
                return new ApiResult<bool>
                {
                    Code = ApiEnums.Error,
                    Msg = $"处理请求时发生错误: {ex.Message}",
                    Data = false
                };
            }
        }

        /// <summary>
        /// 更新设备信息
        /// </summary>
        /// <param name="dto">设备更新数据</param>
        /// <returns>操作结果</returns>
        [HttpPut]
        public async Task<ApiResult<bool>> UpdateEquipment([FromBody] UpdateEquipmentDto dto)
        {
            try
            {
                var command = new UpdateEquipmentCommand { EquipmentData = dto };
                var result = await _mediator.Send(command);

                logger.LogInformation("设备信息更新请求处理完成");

                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "处理更新设备信息请求时发生异常");
                return new ApiResult<bool>
                {
                    Code = ApiEnums.Error,
                    Msg = $"处理请求时发生错误: {ex.Message}",
                    Data = false
                };
            }
        }

        #endregion

        #region 货架字典

        /// <summary>
        /// 添加货架
        /// </summary>
        /// <param name="createShelvesDto">货架数据</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public ApiResult<int> AddShelves([FromBody] CreateShelvesDto createShelvesDto)
        {
            try
            {
                var command = new CreateShelvesCommand(createShelvesDto);
                var result = _mediator.Send(command).GetAwaiter().GetResult();

                logger.LogInformation("货架添加请求处理完成");

                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "处理添加货架请求时发生异常");
                return new ApiResult<int>
                {
                    Code = ApiEnums.Error,
                    Msg = $"货架添加请求处理异常{ex.Message}",
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 修改货架状态
        /// </summary>
        /// <param name="statusDto">状态修改数据</param>
        /// <returns>操作结果</returns>
        [HttpPut("UpdateShelvesStatus")]
        public ApiResult<bool> UpdateShelvesStatus([FromBody] UpdateShelvesStatusDto statusDto)
        {
            try
            {
                var command = new UpdateShelvesStatusCommand(statusDto);
                var result = _mediator.Send(command).GetAwaiter().GetResult();

                logger.LogInformation("货架状态修改请求处理完成");

                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "处理货架状态修改请求时发生异常");
                return new ApiResult<bool>
                {
                    Code = ApiEnums.Error,
                    Msg = $"货架状态修改请求处理异常{ex.Message}",
                    Data = false
                };
            }
        }

        /// <summary>
        /// 更新货架信息
        /// </summary>
        /// <param name="updateShelvesDto">货架更新数据</param>
        /// <returns>操作结果</returns>
        [HttpPut]
        public async Task<ApiResult<bool>> UpdateShelves([FromBody] UpdateShelvesDto updateShelvesDto)
        {
            try
            {
                logger.LogInformation("货架信息更新请求处理完成");
                var command = new UpdateShelvesCommand { ShelvesData = updateShelvesDto };
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "处理货架状态修改请求时发生异常");
                throw;
            }
        }

        /// <summary>
        /// 删除货架(逻辑删除)
        /// </summary>
        /// <param name="id">货架ID</param>
        /// <returns>操作结果</returns>
        [HttpDelete("{id}")]
        public async Task<ApiResult<bool>> DeleteShelves(int id)
        {
            try
            {
                var command = new DeleteShelvesCommand { Id = id };
                var result = await _mediator.Send(command);

                logger.LogInformation("货架删除请求处理完成");

                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "处理删除货架请求时发生异常");
                return new ApiResult<bool>
                {
                    Code = ApiEnums.Error,
                    Msg = $"处理请求时发生错误: {ex.Message}",
                    Data = false
                };
            }
        }

        #endregion

        #region 器械字典

        /// <summary>
        /// 添加器械
        /// </summary>
        /// <param name="dto">器械数据</param>
        /// <returns>添加结果</returns>
        [HttpPost("AddApparatus")]
        public async Task<ApiResult<int>> AddApparatus([FromBody] CreateApparatusDto dto)
        {
            try
            {
                var command = new CreateApparatusCommand(dto);
                var result = await _mediator.Send(command);

                logger.LogInformation("器械添加请求处理完成");

                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "处理添加器械请求时发生异常");
                return new ApiResult<int>
                {
                    Code = ApiEnums.Error,
                    Msg = $"处理请求时发生错误: {ex.Message}",
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 删除器械(逻辑删除)
        /// </summary>
        /// <param name="id">器械ID</param>
        /// <returns>操作结果</returns>
        [HttpDelete("{id}")]
        public async Task<ApiResult<bool>> DeleteApparatus(int id)
        {
            try
            {
                var command = new DeleteApparatusCommand { Id = id };
                var result = await _mediator.Send(command);

                logger.LogInformation("器械删除请求处理完成");

                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "处理删除器械请求时发生异常");
                return new ApiResult<bool>
                {
                    Code = ApiEnums.Error,
                    Msg = $"处理请求时发生错误: {ex.Message}",
                    Data = false
                };
            }
        }

        /// <summary>
        /// 更新器械信息
        /// </summary>
        /// <param name="dto">器械更新数据</param>
        /// <returns>操作结果</returns>
        [HttpPut("UpdateApparatus")]
        public async Task<ApiResult<bool>> UpdateApparatus([FromBody] UpdateApparatusDto dto)
        {
            try
            {
                var command = new UpdateApparatusCommand { ApparatusData = dto };
                var result = await _mediator.Send(command);

                logger.LogInformation("器械信息更新请求处理完成");
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "处理更新器械信息请求时发生异常");
                return new ApiResult<bool>
                {
                    Code = ApiEnums.Error,
                    Msg = $"处理请求时发生错误: {ex.Message}",
                    Data = false
                };
            }
        }

        #endregion

        #region 器械类型字典

        /// <summary>
        /// 添加器械分类
        /// </summary>
        /// <param name="dto">器械分类信息</param>
        /// <returns>添加结果</returns>
        [HttpPost("AddApparatusType")]
        public async Task<ApiResult<int>> AddApparatusType([FromBody] CreateApparatusTypeDto dto)
        {
            var command = new CreateApparatusTypeCommand(dto);
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 更新器械分类
        /// </summary>
        /// <param name="dto">器械分类更新数据</param>
        /// <returns>操作结果</returns>
        [HttpPut("UpdateApparatusType")]
        public async Task<ApiResult<bool>> UpdateApparatusType([FromBody] UpdateApparatusTypeDto dto)
        {
            try
            {
                var command = new UpdateApparatusTypeCommand { ApparatusTypeData = dto };
                var result = await _mediator.Send(command);

                logger.LogInformation("器械分类更新请求处理完成");

                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "处理更新器械分类请求时发生异常");
                return new ApiResult<bool>
                {
                    Code = ApiEnums.Error,
                    Msg = $"处理请求时发生错误: {ex.Message}",
                    Data = false
                };
            }
        }

        /// <summary>
        /// 删除器械分类(逻辑删除)
        /// </summary>
        /// <param name="id">器械分类ID</param>
        /// <returns>操作结果</returns>
        [HttpDelete("DeleteApparatusType/{id}")]
        public async Task<ApiResult<bool>> DeleteApparatusType(int id)
        {
            try
            {
                var command = new DeleteApparatusTypeCommand { Id = id };
                var result = await _mediator.Send(command);

                logger.LogInformation("器械分类删除请求处理完成");

                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "处理删除器械分类请求时发生异常");
                return new ApiResult<bool>
                {
                    Code = ApiEnums.Error,
                    Msg = $"处理请求时发生错误: {ex.Message}",
                    Data = false
                };
            }
        }

        #endregion

        #region 器械包字典

        /// <summary>
        /// 添加器械包
        /// </summary>
        /// <param name="dto">器械包数据</param>
        /// <returns>添加结果</returns>
        [HttpPost("AddEquipmentPackage")]
        public async Task<ApiResult<int>> AddEquipmentPackage([FromBody] CreateEquipmentPackageDto dto)
        {
            try
            {
                var command = new CreateEquipmentPackageCommand(dto);
                var result = await _mediator.Send(command);

                logger.LogInformation("器械包添加请求处理完成");

                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "处理添加器械包请求时发生异常");
                return new ApiResult<int>
                {
                    Code = ApiEnums.Error,
                    Msg = $"处理请求时发生错误: {ex.Message}",
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 更新器械包信息
        /// </summary>
        /// <param name="dto">器械包更新数据</param>
        /// <returns>操作结果</returns>
        [HttpPut("UpdateEquipmentPackage/UpdateEquipmentPackage")]
        public async Task<ApiResult<bool>> UpdateEquipmentPackage([FromBody] UpdateEquipmentPackageDto dto)
        {
            try
            {
                var command = new UpdateEquipmentPackageCommand { PackageData = dto };
                var result = await _mediator.Send(command);

                logger.LogInformation("器械包信息更新请求处理完成");

                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "处理更新器械包信息请求时发生异常");
                return new ApiResult<bool>
                {
                    Code = ApiEnums.Error,
                    Msg = $"处理请求时发生错误: {ex.Message}",
                    Data = false
                };
            }
        }

        /// <summary>
        /// 更新器械包状态
        /// </summary>
        /// <param name="dto">状态更新数据</param>
        /// <returns>操作结果</returns>
        [HttpPut("UpdateEquipmentPackageStatus")]
        public async Task<ApiResult<bool>> UpdateEquipmentPackageStatus([FromBody] UpdateEquipmentPackageStatusDto dto)
        {
            try
            {
                var command = new UpdateEquipmentPackageStatusCommand(dto);
                var result = await _mediator.Send(command);

                logger.LogInformation("器械包状态更新请求处理完成");

                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "处理更新器械包状态请求时发生异常");
                return new ApiResult<bool>
                {
                    Code = ApiEnums.Error,
                    Msg = $"处理请求时发生错误: {ex.Message}",
                    Data = false
                };
            }
        }

        /// <summary>
        /// 删除器械包(逻辑删除)
        /// </summary>
        /// <param name="id">器械包ID</param>
        /// <returns>操作结果</returns>
        [HttpDelete("DeleteEquipmentPackage/DeleteEquipmentPackage/{id}")]
        public async Task<ApiResult<bool>> DeleteEquipmentPackage(int id)
        {
            try
            {
                var command = new DeleteEquipmentPackageCommand { Id = id };
                var result = await _mediator.Send(command);

                logger.LogInformation("器械包删除请求处理完成");

                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "处理删除器械包请求时发生异常");
                return new ApiResult<bool>
                {
                    Code = ApiEnums.Error,
                    Msg = $"处理请求时发生错误: {ex.Message}",
                    Data = false
                };
            }
        }

        #endregion
    }
}